Skip to contentMethod: isInferred(Axiom, Set)
1: /**
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.*;
18: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
20: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
21: import cz.cvut.kbss.ontodriver.model.Axiom;
22: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
23: import cz.cvut.kbss.ontodriver.owlapi.list.OwlapiLists;
24:
25: import java.net.URI;
26: import java.util.Collection;
27: import java.util.List;
28: import java.util.Objects;
29: import java.util.Set;
30:
31: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.getNPXMessageSupplier;
32:
33: /**
34: * Default implementation of the {@link Connection} interface for OWLAPI driver.
35: */
36: public class OwlapiConnection implements Connection {
37:
38: private boolean open;
39: private boolean autoCommit;
40:
41: private final OwlapiAdapter adapter;
42:
43: private OwlapiTypes types;
44: private OwlapiProperties properties;
45: private OwlapiLists lists;
46:
47: private ConnectionListener listener;
48:
49: OwlapiConnection(OwlapiAdapter adapter) {
50: this.adapter = adapter;
51: this.open = true;
52: this.autoCommit = false;
53: }
54:
55: void setListener(ConnectionListener listener) {
56: ensureOpen();
57: assert listener != null;
58: this.listener = listener;
59: }
60:
61: void removeListener() {
62: this.listener = null;
63: }
64:
65: void setTypes(OwlapiTypes types) {
66: this.types = types;
67: }
68:
69: void setProperties(OwlapiProperties properties) {
70: this.properties = properties;
71: }
72:
73: void setLists(OwlapiLists lists) {
74: this.lists = lists;
75: }
76:
77: @Override
78: public boolean isOpen() {
79: return open;
80: }
81:
82: @Override
83: public void commit() {
84: ensureOpen();
85: adapter.commit();
86: }
87:
88: public void ensureOpen() {
89: if (!open) {
90: throw new IllegalStateException("Connection is closed.");
91: }
92: }
93:
94: @Override
95: public void rollback() {
96: ensureOpen();
97: adapter.rollback();
98: }
99:
100: @Override
101: public void setAutoCommit(boolean autoCommit) {
102: this.autoCommit = autoCommit;
103: }
104:
105: @Override
106: public boolean isAutoCommit() {
107: return autoCommit;
108: }
109:
110: @Override
111: public Statement createStatement() {
112: ensureOpen();
113: return adapter.createStatement(this);
114: }
115:
116: @Override
117: public PreparedStatement prepareStatement(String sparql) {
118: ensureOpen();
119: return adapter.prepareStatement(sparql, this);
120: }
121:
122: @Override
123: public boolean isConsistent(URI context) {
124: ensureOpen();
125: return adapter.isConsistent(context);
126: }
127:
128: @Override
129: public List<URI> getContexts() {
130: ensureOpen();
131: return adapter.getContexts();
132: }
133:
134: @Override
135: public boolean contains(Axiom<?> axiom, Set<URI> contexts) {
136: ensureOpen();
137: Objects.requireNonNull(axiom, getNPXMessageSupplier("axiom"));
138: return adapter.containsAxiom(axiom, contexts);
139: }
140:
141: @Override
142: public boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
143: ensureOpen();
144: Objects.requireNonNull(axiom);
145: return adapter.isInferred(axiom, contexts);
146: }
147:
148: @Override
149: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws OntoDriverException {
150: ensureOpen();
151: Objects.requireNonNull(descriptor);
152: try {
153: return adapter.find(descriptor);
154: } catch (RuntimeException e) {
155: throw new OwlapiDriverException(e);
156: }
157: }
158:
159: @Override
160: public void persist(AxiomValueDescriptor descriptor) throws OntoDriverException {
161: ensureOpen();
162: Objects.requireNonNull(descriptor);
163: try {
164: adapter.persist(descriptor);
165: commitIfAuto();
166: } catch (RuntimeException e) {
167: throw new OwlapiDriverException(e);
168: }
169: }
170:
171: @Override
172: public URI generateIdentifier(URI classUri) {
173: ensureOpen();
174: Objects.requireNonNull(classUri);
175: return adapter.generateIdentifier(classUri);
176: }
177:
178: @Override
179: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
180: ensureOpen();
181: Objects.requireNonNull(descriptor);
182: try {
183: adapter.update(descriptor);
184: commitIfAuto();
185: } catch (RuntimeException e) {
186: throw new OwlapiDriverException(e);
187: }
188: }
189:
190: @Override
191: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
192: ensureOpen();
193: Objects.requireNonNull(descriptor);
194: try {
195: adapter.remove(descriptor);
196: commitIfAuto();
197: } catch (RuntimeException e) {
198: throw new OwlapiDriverException(e);
199: }
200: }
201:
202: @Override
203: public Lists lists() {
204: ensureOpen();
205: assert lists != null;
206: return lists;
207: }
208:
209: @Override
210: public Types types() {
211: ensureOpen();
212: assert types != null;
213: return types;
214: }
215:
216: @Override
217: public Properties properties() {
218: ensureOpen();
219: assert properties != null;
220: return properties;
221: }
222:
223: @Override
224: public void close() {
225: if (!open) {
226: return;
227: }
228: if (listener != null) {
229: listener.connectionClosed(this);
230: }
231: this.open = false;
232: }
233:
234: public void commitIfAuto() {
235: if (autoCommit) {
236: adapter.commit();
237: }
238: }
239:
240: @Override
241: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
242: if (cls.isAssignableFrom(this.getClass())) {
243: return cls.cast(this);
244: }
245: return adapter.unwrap(cls);
246: }
247: }